package com.mc.developmentkit.activitys;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import com.qamaster.android.R;
import com.mc.developmentkit.bean.UserInfo;
import com.mc.developmentkit.config.MCConstant;
import com.mc.developmentkit.https.HttpUtils;
import com.mc.developmentkit.json.Json;
import com.mc.developmentkit.utils.Autjcode;
import com.mc.developmentkit.utils.DbUtils;
import com.mc.developmentkit.utils.MCUtils;
import com.mc.developmentkit.utils.ToastUtil;
import com.mc.developmentkit.views.TitleManger;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.DbManager;
import org.xutils.ex.DbException;

/**
 * Created by LeBron on 2017/1/3.
 */

public class AccountSecurityActivity extends Activity implements View.OnClickListener {

    private EditText et_yuanmima, et_xinmima, et_querenxinmima, et_yanzhengma;
    private ImageView img_code;
    private Button btn_xiugaimima;
    private String code_resh;
    private DbManager db;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_security);
        TitleManger insetance = TitleManger.getInsetance();
        insetance.setContext(this);
        insetance.setName("账号安全");
        initView();
    }

    private void initView() {
        et_yuanmima = (EditText) findViewById(R.id.yuanmima);
        et_xinmima = (EditText) findViewById(R.id.xinmima);
        et_querenxinmima = (EditText) findViewById(R.id.querenxinmima1);
        et_yanzhengma = (EditText) findViewById(R.id.yanzhengma);
        img_code = (ImageView) findViewById(R.id.suijiyanzhengma);
        btn_xiugaimima = (Button) findViewById(R.id.xiugaimima);
        img_code.setOnClickListener(this);
        btn_xiugaimima.setOnClickListener(this);
        img_code.setImageBitmap(Autjcode.getInstance().createBitmap());
        code_resh = Autjcode.getInstance().getCode().toLowerCase();
    }

    @Override
    public void onClick(View v) {
        String laomima = et_yuanmima.getText().toString().trim();
        String xinmima = et_xinmima.getText().toString().trim();
        String querenxinmima = et_querenxinmima.getText().toString().trim();
        String yanzhengma = et_yanzhengma.getText().toString().trim();
        int i = v.getId();
        if (i == R.id.suijiyanzhengma) {
            img_code.setImageBitmap(Autjcode.getInstance().createBitmap());
            code_resh = Autjcode.getInstance().getCode().toLowerCase();
        } else if (i == R.id.xiugaimima) {
            if (laomima.length() > 5 && xinmima.length() > 5 && !laomima.equals(xinmima) && !laomima.equals(querenxinmima) && xinmima.equals(querenxinmima) && yanzhengma.toLowerCase().equals(code_resh)) {
                try {
                    JSONObject jsonObject = new JSONObject();
                    jsonObject.put("user_id", MCUtils.LoginUser().id);
                    jsonObject.put("code", "pwd");
                    jsonObject.put("password", xinmima);
                    jsonObject.put("old_password", laomima);
                    HttpUtils.POST(mHandler, MCConstant.getUserUpdatePasswordUrl(), jsonObject.toString(), true);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            } else if (!yanzhengma.toLowerCase().equals(code_resh)) {
                ToastUtil.showToast("验证码输入有误");
            } else if (laomima.length() < 6 || xinmima.length() < 6 || querenxinmima.length() < 6) {
                ToastUtil.showToast("密码长度应大于六位");
            } else if (!xinmima.equals(querenxinmima)) {
                ToastUtil.showToast("新密码和确认密码输入不一致");
            } else if (laomima.equals(xinmima)) {
                ToastUtil.showToast("旧密码与新密码不能一致");
            }

        }
    }

    /**
     * 手机号、普通账号用户 修改密码
     */
    Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1:
                    boolean istrue = Json.DNSJudge("修改密码", msg.obj.toString());
                    if (istrue == true) {
                        if (UserCenterActivity.context!=null){
                            UserCenterActivity.context.finish();
                        }
                        try {
                            db = DbUtils.getDB();
                            db.delete(UserInfo.class);
                        } catch (DbException e) {
                            e.printStackTrace();
                        }
                        ToastUtil.showToast("修改密码成功,请重新登录");
                        startActivity(new Intent(AccountSecurityActivity.this, LoginActivity.class));
                        finish();
                    }
                    break;
                case 2:
                    ToastUtil.showToast("网络异常");
                    break;
            }
        }
    };


}
